<?php
/**
 * Created by PhpStorm.
 * User: yuyiwen
 * Date: 2018/10/9
 * Time: 下午2:21
 */

namespace app\admin\controller;

use think\Request;
use think\Image;
class Product extends Common
{
    public function index ()
    {

        $search = input('get.search' , '' , 'htmlspecialchars');
        $page_search = $search;
        $product = db('product');
        if ($search){

           $search = '%'.$search.'%';


            $where = '';
            $where .= ' `name` LIKE "'.$search;

            $product->where($where);

        }
        $product_list = $product->paginate(2 , false , ['query'=>['search'=>$page_search]]);

        $current_page = input('get.page' , 1 , 'intval');

        $page = $product_list->render();
        $page = str_replace(
            ['class="pagination"' , 'class="disabled"' , 'class="active"'],
            ['class="am-pagination tpl-pagination"' , 'class="am-disabled"' , 'class="am-active"'],
            $page
        );
        $request = Request::instance();
        if ( $request->isAjax() )
        {
            return json(['product_list' => $product_list  , 'page' => $page , 'current_page'=>$current_page]);
        }
        $this->assign('page' , $page);
        $this->assign('current_page' , $current_page);
        $this->assign('product_list' , $product_list);
        return view();
    }

    public function create ()
    {
        return view();
    }


    public function store ()
    {
        // 文件上传
        $file = request()->file('cover_img');
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');

            if($info){
                $post['cover_img']  = $info->getSaveName();

                # 制作缩略图
                $image  = Image::open(ROOT_PATH . 'public' . DS . 'uploads/' . $post['cover_img']);
//                print_r($image);die;
                if (!file_exists(dirname(ROOT_PATH . 'public' . DS .'/uploads/thumb/'. $post['cover_img']))){
                    mkdir(dirname(ROOT_PATH . 'public' . DS .'/uploads/thumb/'. $post['cover_img']) , 0777, true);
                }
                $image->thumb('200' , '100' , Image::THUMB_FILLED) -> save( ROOT_PATH . 'public' . DS .'/uploads/thumb/'. $post['cover_img'] , null , 20);
            }else{
                $this->error( $file->getError() );
            }
        }
        $post['name'] = input('post.name' , '' , 'htmlspecialchars');
        $post['commission'] = input('post.commission' , '' , 'htmlspecialchars');
        $post['bounds'] = input('post.bounds' , '' , 'htmlspecialchars');
        $post['cat_id'] = input('post.cat_id' , '' , 'htmlspecialchars');
        $post['price'] = input('post.price' , '' , 'htmlspecialchars');
        $post['detail'] = input('post.detail' , '' , 'htmlspecialchars');
//        dump($post);die;
        $id = db('product')->insertGetId($post);
        if($id){
            $this->success('新增产品成功' , url('index'));
        }

    }

    # 规格管理页面
    public function guige () {
        # 接收产品id
        $id = input('id' , '' , 'intval');
        $pg = model('ProductGuige');

        $guige = $pg->getProductGuige($id);
//        dump($guige);die;
        $this->assign('guige' , $guige);
        return view();
    }

    # 添加规格的方法
    public function guige_add () {
        # 接收产品id
        $data['product_id'] = input('get.product_id');
        # 接收规格父id
        $data['pid'] = input('get.pid');

        $id = db('ProductGuige')->insertGetId($data);
        if ($id) {
            return json(['msg'=> 'ok' , 'guige_id'=>$id]);
        }else {
            return json(['msg'=>'fail']);
        }
    }

    public function guige_store () {
        $name = input('get.name');
        $guige_list = json_decode($name , true);
        foreach($guige_list as $key => $value) {
            db('product_guige')->where('guige_id' , $value['guige_id'])->update($value);
        }
    }



    public function guige_del () {
        $guige_id = input('get.guige_id');
        # 查出当前规格的二级规格
        $erjiguige = db('ProductGuige')->where('pid' , $guige_id)->select();
        # 循环二级规格
        foreach($erjiguige as $key => $value) {
            # 删除三级规格
            db('ProductGuige')->where('pid' , $value['guige_id'])->delete();
            # 删除二级规格
            db('ProductGuige')->where('guige_id' , $value['guige_id'])->delete();
        }
        # 删除一级规格
        db('ProductGuige')->where('guige_id' , $guige_id)->delete();
    }

    public function guige_xiang_add () {
        $data['guige_id'] = input('get.guige_id');
        $data['product_id'] = input('get.product_id');
        $id = db('ProductGuigeAttr')->insertGetId($data);
        if ($id) {
            return json(['msg'=> 'ok' , 'attr_id'=>$id]);
        }else {
            return json(['msg'=>'fail']);
        }
    }

    public function guige_attr_store () {
        $name = input('get.name');
        $attr_list = json_decode($name , true);
        foreach($attr_list as $key => $value) {
            db('ProductGuigeAttr')->where('attr_id' , $value['attr_id'])->update($value);
        }
    }

    public function guige_attr_del () {
        $guige_id = input('get.attr_id');
        db('ProductGuigeAttr')->where('attr_id' , $guige_id)->delete();
    }

    public function guding_store ()
    {
        $post = input('post.');
        $product_id = $post['product_id'];
        $title = $post['title'];
        unset($post['product_id']);
        unset($post['title']);

        foreach ($post as $key => $value) {
            if ( isset($value[0]) ) {
                $attrInfo = db('ProductGuigeAttr')->where(['product_id' => $product_id , 'attr_value'=>$value[0]])->find();
                #$attrInfo['attr_id']
                $post[$key]['fir_guige'] = $attrInfo['attr_id'];
                unset($post[$key][0]);
            }
            if ( isset($value[1]) ) {
                $attrInfo = db('ProductGuigeAttr')->where(['product_id' => $product_id , 'attr_value'=>$value[1]])->find();
                #$attrInfo['attr_id']
                $post[$key]['sec_guige'] = $attrInfo['attr_id'];
                unset($post[$key][1]);
            }
            if ( isset($value[2]) ) {
                $attrInfo = db('ProductGuigeAttr')->where(['product_id' => $product_id , 'attr_value'=>$value[2]])->find();
                #$attrInfo['attr_id']
                $post[$key]['thi_guige'] = $attrInfo['attr_id'];
                unset($post[$key][2]);
            }
            db('product_guding_attr')->insert($post[$key]);
        }

        dump($post);
    }

    public function test () {
        return view();
    }
}